<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
FlyUI Overview
</title>
<link rel ="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script>
function asd() {
	
		parent.document.title="mvcutils-model2.js Overview";
	
}
</script>
</head>
<body bgcolor="white" onload="asd();">

<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> 	<font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top">
<em>
<b>FlyUI</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<center>
	
	   <h2>mvcutils-model2.js</h2>
	
</center>

	


<h4>Summary</h4>
<p>
	
		This script defines a generic model class for MVC widgets and applications.
 <BR/><BR/><B>Version: </B>$Revision:538 $ on $Date: 2008-08-14 10:36:17 +0100 (Thu, 14 Aug 2008) $ by $Author: aliman $
 TODO license terms<BR/><BR/><B>Requires:</B><UL>- YAHOO.util.CustomEvent</UL><B>Author:</B> <a href="http://purl.org/net/aliman">Alistair Miles</a>
 <BR/>
	
</p>

<hr>


    <table border="1" cellpadding="3" cellspacing="0" width="100%">
    <tr bgcolor="#CCCCFF" class="TableHeadingColor">
    <td colspan=2><font size="+2">
    
        <b>Class Summary</b>
    
    </font></td>
    </tr>
    
    <tr bgcolor="white" class="TableRowColor">
    <td width="15%"><b><a href="flyui/mvcutils/GenericModel2.html">flyui.mvcutils.GenericModel2</a></b></td>
    <td>A generic model class for an MVC widget or application.</td>
    </tr>
    
    </table>
    <hr/> 


<!-- ========== METHOD SUMMARY =========== -->

<!-- ========== END METHOD SUMMARY =========== -->


        <pre class="sourceview"><span class="comment">/**
 * <span class="attrib">@fileoverview</span>
 * This script defines a generic model class for MVC widgets and applications.
 * <span class="attrib">@requires</span> YAHOO.util.CustomEvent
 * <span class="attrib">@author</span> &lt;a href="http://purl.org/net/aliman"&gt;Alistair Miles&lt;/a&gt;
 * <span class="attrib">@version</span> $Revision:538 $ on $Date: 2008-08-14 10:36:17 +0100 (Thu, 14 Aug 2008) $ by $Author: aliman $
 * TODO license terms
 */</span>
 
 
<span class="comment">// create a namespace if not already defined</span>
flyui.namespace(<span class="literal">"flyui.mvcutils"</span>);


<span class="comment">/** 
 * Create a new instance.
 * <span class="attrib">@class</span>
 * A generic model class for an MVC widget or application.
 * <span class="attrib">@constructor</span>
 */</span>
flyui.mvcutils.GenericModel2 = <span class="reserved">function</span>() {};
	
<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._state = null;
	
<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._events = null;

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._properties = null;

<span class="comment">/**
 * <span class="attrib">@private</span>
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._values = null;


<span class="comment">/**
 * TODO doc me
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>.setDefinition = <span class="reserved">function</span>( definition ) {
	<span class="reserved">this</span>._init(definition);	
}


<span class="comment">/**
 * TODO doc me
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._init = <span class="reserved">function</span>( definition ) {

	<span class="reserved">if</span> (typeof definition == <span class="literal">"object"</span>) {

		<span class="reserved">if</span> (typeof definition.properties == <span class="literal">"object"</span> &amp;&amp; definition.properties.length) {
			<span class="comment">// store properties</span>
			<span class="reserved">this</span>._properties = definition.properties;
			<span class="comment">// init events</span>
			<span class="reserved">this</span>._events = {};
			<span class="reserved">for</span> (var i=0; i&lt;<span class="reserved">this</span>._properties.length; i++) {
				var type = <span class="reserved">this</span>._properties[i];
				<span class="reserved">this</span>._events[type] = new YAHOO.util.CustomEvent(type, <span class="reserved">this</span>);
			}	
		}
		<span class="reserved">else</span> throw new flyui.mvcutils.GenericModel2.BadDefinition(<span class="literal">"definition must have 'properties' field with array value"</span>);
		
		<span class="comment">// store values</span>
		<span class="reserved">if</span> (typeof definition.values == <span class="literal">"object"</span>) {
			<span class="reserved">this</span>._values = definition.values;
		}
		<span class="reserved">else</span> throw new flyui.mvcutils.GenericModel2.BadDefinition(<span class="literal">"definition must have 'values' field with object value"</span>);
		
		<span class="comment">// init state</span>
		<span class="reserved">this</span>._state = {};
		<span class="reserved">if</span> (typeof definition.initialize == <span class="literal">"function"</span>) {
			definition.initialize(<span class="reserved">this</span>._state);
		}
		<span class="reserved">else</span> throw new flyui.mvcutils.GenericModel2.BadDefinition(<span class="literal">"definition must have 'initialize' field with function value"</span>);		
		
	}
	<span class="reserved">else</span> throw new flyui.mvcutils.GenericModel2.BadDefinition(<span class="literal">"definition must be an object"</span>);		
	
}


<span class="comment">/**
 * TODO doc me
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>.subscribe = <span class="reserved">function</span>( property, listener, obj ) {

	<span class="comment">// guard condition</span>
	<span class="reserved">this</span>._validateProperty(property);

	<span class="comment">// do subscription</span>
	<span class="reserved">this</span>._events[property].subscribe(listener, obj);
		
}


<span class="comment">/**
 * TODO doc me
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>.subscribeAll = <span class="reserved">function</span>( listener, obj ) {

	<span class="reserved">for</span> (var i=0; i&lt;<span class="reserved">this</span>._properties.length; i++) {
		var property = <span class="reserved">this</span>._properties[i];
		<span class="comment">// pass through</span>
		<span class="reserved">this</span>.subscribe(property, listener, obj);
	}
	
}


<span class="comment">/**
 * TODO doc me
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>.get = <span class="reserved">function</span>( property ) {

	<span class="comment">// guard condition</span>
	<span class="reserved">this</span>._validateProperty(property);

	<span class="reserved">return</span> <span class="reserved">this</span>._state[property];
	
};


<span class="comment">/**
 * TODO doc me
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>.getter = <span class="reserved">function</span>() {
    var self = <span class="reserved">this</span>;
    <span class="reserved">return</span> <span class="reserved">function</span>( property ) {
        <span class="reserved">return</span> self.get(property);
    }
};


<span class="comment">/**
 * TODO doc me
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>.set = <span class="reserved">function</span>( property, to ) {

	<span class="comment">// guard condition</span>
	<span class="reserved">this</span>._validatePropertyValue(property, to);
	
	<span class="comment">// store old value temporarily</span>
	var from = <span class="reserved">this</span>._state[property];
	
	<span class="comment">// set new value</span>
	<span class="reserved">this</span>._state[property] = to;
	
	<span class="comment">// notify listeners</span>
	<span class="reserved">this</span>._notify(property, from, to);

}


<span class="comment">/**
 * TODO doc me
 * <span class="attrib">@private</span>
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._validateProperty = <span class="reserved">function</span>( property ) {

	var valid = false;

	<span class="reserved">for</span> (var i=0; i&lt;<span class="reserved">this</span>._properties.length; i++) {
		<span class="reserved">if</span> (<span class="reserved">this</span>._properties[i] == property) valid = true;
	}

	<span class="reserved">if</span> (!valid) {
		throw new flyui.mvcutils.GenericModel2.BadPropertyName(<span class="literal">"bad property name: "</span>+property);
	}

}


<span class="comment">/**
 * TODO doc me
 * <span class="attrib">@private</span>
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._validatePropertyValue = <span class="reserved">function</span>( property, value ) {

	var valid = false;

	<span class="comment">// check property first</span>
	<span class="reserved">this</span>._validateProperty(property);
	
	<span class="comment">// check values</span>
	var values = <span class="reserved">this</span>._values[property];
	
	<span class="reserved">if</span> (values) {
		<span class="reserved">for</span> (var i in values) {
			<span class="reserved">if</span> (value == values[i]) {
				valid = true;
			}	
		}
	} <span class="reserved">else</span> {
		<span class="comment">// true if no values defined</span>
		valid = true;
	}

	<span class="reserved">if</span> (!valid) {
		throw new flyui.mvcutils.GenericModel2.BadPropertyValue(<span class="literal">"bad property value, property: "</span>+property+<span class="literal">", value: "</span>+value);
	}
	
}


<span class="comment">/**
 * TODO doc me
 * <span class="attrib">@private</span>
 */</span>
flyui.mvcutils.GenericModel2.<span class="reserved">prototype</span>._notify = <span class="reserved">function</span>( property, from, to ) {

	<span class="comment">// property must already have been checked</span>
	<span class="reserved">this</span>._events[property].fire(from, to, <span class="reserved">this</span>.getter());
};

flyui.mvcutils.GenericModel2.BadDefinition = <span class="reserved">function</span>( message ) {
	<span class="reserved">this</span>.name = <span class="literal">"flyui.mvcutils.GenericModel2.BadDefinition"</span>;
	<span class="reserved">this</span>.message = message;
};

flyui.mvcutils.GenericModel2.BadPropertyName = <span class="reserved">function</span>( message ) {
	<span class="reserved">this</span>.name = <span class="literal">"flyui.mvcutils.GenericModel2.BadPropertyName"</span>;
	<span class="reserved">this</span>.message = message;
};

flyui.mvcutils.GenericModel2.BadPropertyValue = <span class="reserved">function</span>( message ) {
	<span class="reserved">this</span>.name = <span class="literal">"flyui.mvcutils.GenericModel2.BadPropertyValue"</span>;
	<span class="reserved">this</span>.message = message;
};

</pre>
	<hr>



<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top"><em>
<b>FlyUI</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<font size="-1">

</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Tue Dec  9 11:08:18 2008</div>
</body>
</html>
